import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/*
class LoginExample extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}
*/
class LoginExample extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginExample> {
  GlobalKey<FormState> loginkey = GlobalKey<FormState>();
  String userName;
  String password;

  void login() {
    var loginForm = loginkey.currentState;

    if (loginForm.validate()) {
      loginForm.save();
      print('_LoginPageState.login userName:' +
          userName +
          ' password:' +
          password);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('form 表单示例'),
        ),
        body: Column(
          children: <Widget>[
            Container(
              padding: const EdgeInsets.all(16.0),
              child: Form(
                key: loginkey,
                child: Column(
                  children: <Widget>[
                    TextFormField(
                      decoration: InputDecoration(labelText: '请输入用户名 '),
                      onSaved: (value) {
                        userName = value;
                      },
                      onFieldSubmitted: (value) {},
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: '请输入密码 '),
                      obscureText: true,
                      validator: (value) {
                        return value.length < 6 ? "密码长度不够6位" : null;
                      },
                      onSaved: (value) {
                        password = value;
                      },
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(
              width: double.infinity,
              height: 42.0,
              child: RaisedButton(
                onPressed: login,
                child: Text(
                  '登录',
                  style: TextStyle(fontSize: 18.0),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

